package com.yonyou.pmclouds.picture.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.entity.PictureWebVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.picture.rmiitf.PictureWebService;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = PictureWebService.class)
public class PictureWebServiceImpl implements PictureWebService {

    @Autowired
    private PictureRemoteService pictureService;

    @Override
    public void upload(PictureWebVO pictureWebVO) throws BusinessException {
        checkSuffix(pictureWebVO.getPictures());
        //新图片入库
        PictureVO[] pictureVOS = pictureService.insertPictureForPub(pictureWebVO);
        if(null == pictureVOS){
            throw new BusinessException("图片上传失败");
        }
    }

    @Override
    public String[] query(String pkBill) throws BusinessException {
        return pictureService.queryByPkBills(pkBill);
    }

    @Override
    public void delete(String pkBill,String picName) throws BusinessException {
        pictureService.deletePictures(pkBill,picName);
    }

    /**
     * 检查文件名后缀
     * @param picNames
     * @throws BusinessException
     */
    private void checkSuffix(String[] picNames) throws BusinessException {
        if(null == picNames || picNames.length == 0){
            throw new BusinessException("上传图片为空");
        }

        for(String picName : picNames){
            String[] picNameAll = picName.split("\\.");
            if(null == picNameAll || picNameAll.length < 2){
                throw new BusinessException("不支持的上传类型");
            }

            String suffix = picNameAll[1].trim();
            if(!(suffix.equals("mp4") || suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("png") || suffix.equals("zip") || suffix.equals("rar") || suffix.equals("doc") || suffix.equals("docx") || suffix.equals("xls") || suffix.equals("xlsx"))){
                throw new BusinessException("不支持的上传类型");
            }
        }

    }
}
